<template>
	<el-container>
		<el-header>
			<div>
				<el-form :inline="true">
					<el-form-item>
						<el-input
							placeholder=""
							v-model="search.queryValue"
							class="input-with-select"
							clearable
						>
							<template v-slot:prepend>
								<!-- slot="prepend" -->
								<el-select
									v-model="search.queryKey"
									placeholder="请选择"
									class="query-select"
									clearable
								>
									<el-option
										label="租户名称"
										value="name"
									></el-option>
									<el-option
										label="租户别名"
										value="alias"
									></el-option>
									<el-option
										label="租户编码"
										value="code"
									></el-option>
								</el-select>
							</template>
						</el-input>
					</el-form-item>
				</el-form>
			</div>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"
					>添加</el-button
				>
				<el-button
					type="danger"
					plain
					icon="el-icon-delete"
					:disabled="selection.length == 0"
					@click="batch_del"
					>批量删除</el-button
				>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-button
						type="primary"
						icon="el-icon-search"
						@click="upsearch"
						>搜索</el-button
					>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable
				ref="table"
				tableName="listCustomColumn"
				:apiObj="apiObj"
				:params="search"
				:column="column"
				row-key="projectId"
				@selection-change="selectionChange"
				stripe
			>
				<el-table-column type="selection" width="50"></el-table-column>
				<template #operator="scope">
					<el-button-group>
						<el-button
							text
							type="primary"
							size="small"
							@click="table_show(scope.row, scope.$index)"
							>查看</el-button
						>
						<el-button
							text
							type="primary"
							size="small"
							@click="table_edit(scope.row, scope.$index)"
							>编辑</el-button
						>
						<el-popconfirm
							title="确定删除吗？"
							@confirm="table_del(scope.row, scope.$index)"
						>
							<template #reference>
								<el-button text type="primary" size="small"
									>删除</el-button
								>
							</template>
						</el-popconfirm>
					</el-button-group>
				</template>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog
		v-if="dialog.save"
		ref="saveDialog"
		@success="handleSaveSuccess"
		@closed="dialog.save = false"
	></save-dialog>
</template>

<script>
import saveDialog from "./save";

export default {
	name: "projectManage",
	components: {
		saveDialog,
	},
	data() {
		return {
			dialog: {
				save: false,
			},
			apiObj: this.$API.tenant.page,
			selection: [],
			search: {
				queryKey: "",
				queryValue: "",
			},
			column: [
				{
					label: "租户名称",
					prop: "tenantName",
					width: "150",
				},
				{
					label: "租户编码",
					prop: "tenantCode",
					width: "200",
				},
				{
					label: "租户别名",
					prop: "tenantAliasName",
					width: "150",
				},
				{
					label: "联系人名称",
					prop: "contactName",
					width: "200",
				},
				{
					label: "联系人电话",
					prop: "contactPhone",
					width: "150",
				},
				{
					label: "过期时间",
					prop: "expireTime",
					width: "200",
				},
				{
					label: "租户地址",
					prop: "tenantAddress",
					width: "150",
				},
				{
					label: "租户邮箱",
					prop: "tenantMail",
					width: "200",
				},
				{
					label: "域名",
					prop: "tenantDomain",
					width: "150",
				},
				{
					label: "logo",
					prop: "logoUrl",
					width: "150",
				},
				{
					label: "操作",
					prop: "operator",
					width: "170",
					fixed: "right",
					align: "right",
				},
			],
		};
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open();
			});
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
		//查看
		table_show(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("show").setData(row);
			});
		},
		//权限设置
		permission() {
			this.dialog.permission = true;
			this.$nextTick(() => {
				this.$refs.permissionDialog.open();
			});
		},
		//删除
		async table_del(row) {
			var reqData = { id: row.tenantId };
			var res = await this.$API.tenant.remove.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//批量删除
		async batch_del() {
			this.$confirm(
				`确定删除选中的 ${this.selection.length} 项吗？`,
				"提示",
				{
					type: "warning",
				}
			)
				.then(() => {
					const loading = this.$loading();
					this.$refs.table.refresh();
					loading.close();
					this.$message.success("操作成功");
				})
				.catch(() => {});
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search);
		},
		//本地更新数据
		handleSaveSuccess(data, mode) {
			if (mode == "add") {
				this.$refs.table.refresh();
			} else if (mode == "edit") {
				this.$refs.table.refresh();
			}
		},
	},
};
</script>

<style>
.query-select {
	max-width: 100px;
}
</style>
